package com.adgroup.smartnote;

import org.json.JSONObject;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

import Net.JsonParse;
import Utilities.Configuration;
import Utilities.Utility;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class ForgetPasswordActivity extends SherlockActivity{

	EditText etEmail;
	Button btnSend;
	private ActionBar mActionBar;
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		return super.onCreateOptionsMenu(menu);
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();			
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.forgetpassword);
		
		//action bar
		mActionBar = getSupportActionBar();
		mActionBar.setDisplayHomeAsUpEnabled(false);
		mActionBar.setDisplayUseLogoEnabled(true);
		mActionBar.setHomeButtonEnabled(true);
		mActionBar.setTitle(getResources().getString(R.string.textviewforgetpassword));
		
		etEmail = (EditText) findViewById(R.id.editTextemailforgetpassword);
		btnSend = (Button) findViewById(R.id.buttonsendforgetpassword);
		
		btnSend.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String email = etEmail.getText().toString();
				//check infor
				if(email.equalsIgnoreCase("")){
					new AlertDialog.Builder(ForgetPasswordActivity.this)
					.setTitle(getResources().getString(R.string.errortitlelogin))
					.setMessage(getResources().getString(R.string.errornullgmailregister))
					.setPositiveButton(getResources().getString(R.string.ok),
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									Utility.ShowKeyboard(etEmail,ForgetPasswordActivity.this);
								}
					}).show();
				}
				else if(!Utility.isEmailValid(email)){
					new AlertDialog.Builder(ForgetPasswordActivity.this)
					.setTitle(getResources().getString(R.string.errortitlelogin))
					.setMessage(getResources().getString(R.string.errorninvalidgmailregister))
					.setPositiveButton(getResources().getString(R.string.ok),
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									Utility.ShowKeyboard(etEmail,ForgetPasswordActivity.this);
								}
					}).show();
				}
				else{
					new AsynSendForget().execute(new String[]{email});
				}
			}
		});
	}
	class AsynSendForget extends AsyncTask<String, Void, String>
	{
		
		private ProgressDialog progress;
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			progress = ProgressDialog.show(ForgetPasswordActivity.this, "",
					getResources().getString(R.string.sendemailforgetpass), true);
		}
		@Override
		protected String doInBackground(String... params) {
			return new JsonParse().SendEmailForgetPassword(params[0]);
		}
		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			progress.dismiss();
			try {
				JSONObject json = new JSONObject(result);
				String r = json.getString(Configuration.result);
				if(r.equalsIgnoreCase("-1")){
					Utility.ShowAlert(ForgetPasswordActivity.this, getResources().getString(R.string.error), getResources().getString(R.string.errormatchemailforgetpass));
				}
				else{
					new AlertDialog.Builder(ForgetPasswordActivity.this)
					.setTitle(getResources().getString(R.string.success))
					.setMessage(getResources().getString(R.string.successsendingemailforgetpass))
					.setPositiveButton(getResources().getString(R.string.ok),
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									finish();
								}
					}).show();
				}
			}
			catch(Exception ex) 
			{
				Log.e("error",ex.getMessage());
			}
		}
	}
}
